#include <stdio.h>
#include <string.h>
//i am from nanjing
//gnijnan morf ma i
//nanjing from am i

//逆序算法(首尾互换)
void Reserve(char *s, int len)
{
    for(int i = 0; i < len / 2; i++)
    {
        char temp = s[i];
        s[i] = s[len - 1 - i];
        s[len-i-1] = temp;
    }
}
void Reserve2(char *s, int len)
{
    int count = 0; //单词长度
    for(int i = 0; i < len; i++)
    {
        if(s[i] == ' ' || s[i] == '\0')
        {
            Reserve(s + i - count,count);  //单词逆序
            count = 0;
        }
        else
        {
            count++;
        }
    }
}

int main()
{
    char str[] = "i am from nanjing";
    Reserve(str, strlen(str));
    Reserve2(str, strlen(str));
    printf("%s\n",str);
    return 0;
}